import java.util.HashMap;

public class L3_3 {

    public static void main(String[] args) {
        String str = "abcabcbb";
//        String str = "pkpm";
        int i = new Solution().lengthOfLongestSubstring(str);
        System.out.println(i);
    }


    static class Solution {
        public int lengthOfLongestSubstring(String s) {
            if (s.length() < 2) {
                return s.length();
            }
            int i = 0;
            int j = 1;
            int result = 0;
            HashMap<Character, Integer> map = new HashMap<>(s.length());
            map.put(s.charAt(i), i);
            while (j < s.length()) {
                Integer index = map.get(s.charAt(j));
                if (null != index) {
                    result = Math.max(result, j - i);
                    while (i <= index) {
                        map.remove(s.charAt(i));
                        i++;
                    }
                }
                map.put(s.charAt(j), j);
                j++;
            }
            return Math.max(result, j - i);
        }
    }
}
